package org.xc.uvc_camera;

import android.text.TextUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.LinkedList;
import java.util.List;

import me.zuichu.mp4coder.Container;
import me.zuichu.mp4coder.muxer.Movie;
import me.zuichu.mp4coder.muxer.Track;
import me.zuichu.mp4coder.muxer.builder.DefaultMp4Builder;
import me.zuichu.mp4coder.muxer.container.mp4.MovieCreator;
import me.zuichu.mp4coder.muxer.tracks.AppendTrack;

public class Mp4Util {

    private Mp4Util() {}
    private static Mp4Util instance;
    synchronized static Mp4Util getInstance() {
        if (instance == null) {
            instance = new Mp4Util();
        }
        return instance;
    }

    String currFileName;
    boolean isUploading = false;

    void start() {
        currFileName = "43499070662630";
        if (!isUploading && !TextUtils.isEmpty(currFileName)) {
            new UploadThread().start();
        }
    }

    private class UploadThread extends Thread {
        @Override
        public void run() {
            super.run();
            isUploading = true;

            try {
                upload(7);
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                Thread.sleep(8 * Constant.DRUATION * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                upload(8);
            } catch (IOException e) {
                e.printStackTrace();
            }

            isUploading = false;
        }
    }

    private void upload(int type) throws IOException {
        List<Movie> movieList = new LinkedList<>();

        List<File> mp4FileList = FileUtil.getFiles(currFileName,type);
        for(File file: mp4FileList) {
            movieList.add(MovieCreator.build(file.getAbsolutePath()));
        }

        List<Track> videoTracks = new LinkedList<>();
        List<Track> audioTracks = new LinkedList<>();
        for (Movie m : movieList) {
            for (Track t : m.getTracks()) {
                if (t.getHandler().equals("soun")) {
                    audioTracks.add(t);
                }
                if (t.getHandler().equals("vide")) {
                    videoTracks.add(t);
                }
            }
        }
        Movie result = new Movie();
        try {
            if (audioTracks.size() > 0) {
                result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
            }
            if (videoTracks.size() > 0) {
                result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Container out = new DefaultMp4Builder().build(result);

        try {
            String rstFilePath = FileUtil.createFile("rst.mp4");
            FileChannel fc = new FileOutputStream(new File(rstFilePath)).getChannel();
            out.writeContainer(fc);
            fc.close();

//            UploadUtil.Companion.uploadFile(rstFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
